using System.Reflection;
using System.Text;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Serilog;
using SqlSugar;
using XjjXmm.Infrastructure.Configuration;
using XjjXmm.Infrastructure.Ioc;
using XjjXmm.Infrastructure.Mongo;
using xjjxmm.infrastructure.repository.impl.sugar;
using xjjxmm.infrastructure.repository.interfaces;
using XjjXmm.Infrastructure.ToolKit;
using XjjXmm.Infrastructure.User;
using xjjxmm.infrastructure.xjjxmmLog.LogKit;
using xjjxmm.infrastructure.xjjxmmLog.AuditLog;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using xjjxmm.infrastructure.Webs;
using XjjXmm.Infrastructure.Webs.ActionResult;

namespace xjjxmm.infrastructure.SetUp
{
    public static class AutoFacStartUp
	{
		public static WebApplicationBuilder RegistAutoFac(this WebApplicationBuilder builder)
		{
			//引入autofac
			builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

			builder.Host.ConfigureContainer<ContainerBuilder>((_, builder2) =>
			{
				builder2.RegisterGeneric(typeof(XjjXmmSeriLogger<>)).As(typeof(ILog<>)).SingleInstance();

				builder2.RegisterType<LoggerInterceptor>().AsSelf().SingleInstance();
				builder2.RegisterType<LoggerAsyncInterceptor>().AsSelf().SingleInstance();

				builder2.RegisterGeneric(typeof(Repository<>))
					.As(typeof(IRepository<>))
					.InstancePerLifetimeScope();
				
				//builder2.RegisterGeneric(typeof(MongoRepository<>)).InstancePerLifetimeScope();

				
				//builder2.RegisterType<UserContext>().As<IUserContext>().InstancePerLifetimeScope();
				builder2.RegisterType<ThreadLocalContext>().InstancePerLifetimeScope();
                builder2.RegisterType<SqlContext>().As<ISqlContext>().InstancePerLifetimeScope();

                builder2.RegisterType<StreamResultExecutor>().As<IActionResultExecutor<StreamResult>>().SingleInstance();
				
                //builder2.RegisterType<AuditLogFliter>().As<IAsyncActionFilter>().InstancePerLifetimeScope();

            });

			return builder;
		}
	}
}
